package controller

import (
	"apitestAfterGo/common"
	"apitestAfterGo/domain"
	"apitestAfterGo/service"
	"apitestAfterGo/utils"
	"github.com/kataras/iris/v12/context"
	"github.com/kataras/iris/v12/mvc"
)

type TestCaseController struct {
}

func (t *TestCaseController) BeforeActivation(b mvc.BeforeActivation) {
	b.Handle("POST", "/addCase", "AddCase")
	b.Handle("PUT", "/modifyTestCase", "ModifyTestCase")
	b.Handle("GET", "/getTestCaseById/{id:int}", "GetTestCaseById")
	b.Handle("DELETE", "/deleteTestCaseById/{id:int}", "DeleteTestCaseById")
}

func (t *TestCaseController) AddCase(context *context.Context, domain domain.TestCase) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	domain.CreateBy = username
	domain.UpdateBy = username
	testCase := service.TestCaseServiceImpl.AddCase(domain)
	return utils.Success(testCase, "添加用例成功！")
}

func (t *TestCaseController) ModifyTestCase(context *context.Context, domain domain.TestCase) common.Result {
	username := utils.GetJwtParameter(context, "username").(string)
	domain.UpdateBy = username
	testCase := service.TestCaseServiceImpl.ModifyTestCase(domain)
	return utils.Success(testCase, "修改用例成功！")
}

func (t *TestCaseController) GetTestCaseById(id int) common.Result {
	testCase := service.TestCaseServiceImpl.GetTestCaseById(id)
	return utils.Success(testCase, "获取用例成功！")
}

func (t *TestCaseController) DeleteTestCaseById(id int) common.Result {
	testCase := service.TestCaseServiceImpl.DeleteTestCaseById(id)
	return utils.Success(testCase, "删除用例成功！")
}
